package top.fengyulou.xiaofeng.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import top.fengyulou.xiaofeng.common.UuidUtil;
import top.fengyulou.xiaofeng.domain.Menu;
import top.fengyulou.xiaofeng.dto.MenuDto;
import top.fengyulou.xiaofeng.enums.MenuStatusEnum;
import top.fengyulou.xiaofeng.mapper.MenuMapper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author  guochao
 * @date  2022/1/10 22:18
 * @version 1.0
 */
@Service
public class MenuService {
    @Resource
    private MenuMapper menuMapper;

    //分页查询
    public void listPage(MenuDto menuDto) {
        //查询数据
        PageHelper.startPage(menuDto.getPageNumber(), menuDto.getPageSize());
        List<MenuDto> list = menuMapper.list(menuDto);
        PageInfo<MenuDto> pageInfo = new PageInfo<>(list);

        //返回结果
        menuDto.setTotal(pageInfo.getTotal());
        menuDto.setRows(list);
    }

    //查询所有菜单列表
    public List<MenuDto> listAll(){
        return menuMapper.list(null);
    }

    //查询菜单列表
    public List<MenuDto> list(MenuDto menuDto){
        return menuMapper.list(menuDto);
    }

    //查询左侧菜单列表
    public List<MenuDto> leftMenuList(){
        MenuDto query = new MenuDto();
        query.setStatus(MenuStatusEnum.SHOW.getCode());
        return menuMapper.list(query);
    }

    //根据id查询
    public MenuDto getById(String id) {
        return menuMapper.getById(id);
    }

    //保存
    public void save(MenuDto menu) {
        if (menu.getId() != null) {
            menu.setUpdateTime(new Date());
            menu.setLastUpdateUserId(menu.getCurrentUserId());
            menuMapper.update(menu);
        }else{
            menu.setId(UuidUtil.getShortUuid());
            menu.setCreateTime(new Date());
            menu.setCreateUserId(menu.getCurrentUserId());
            menuMapper.insert(menu);
        }
    }

    //删除
    public void delete(String[] ids) {
        menuMapper.delete(ids);
    }

    //修改显示状态
    public void updateStatus(String id,Integer status,String userId) {
        MenuDto menu = new MenuDto();
        menu.setId(id);
        menu.setStatus(status);
        menu.setUpdateTime(new Date());
        menu.setLastUpdateUserId(userId);
        menuMapper.update(menu);
    }

    //修改排序
    public void updateSort(String id,Integer sort,String userId) {
        MenuDto menu = new MenuDto();
        menu.setId(id);
        menu.setSort(sort);
        menu.setUpdateTime(new Date());
        menu.setLastUpdateUserId(userId);
        menuMapper.update(menu);
    }
}
